package org.syntaxlisp.core.element;

import lombok.Getter;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HuangYijun
 * @data 2023-02-03 11:41
 **/
public class AmbiguousOrElement extends SyntaxLispElement {

    @Getter
    private final List<ReferenceElement> refs;

    public AmbiguousOrElement(List<ReferenceElement> refs, List<SyntaxLispElement> subElements) {
        super(SyntaxLispElementType.AMBIGUOUS_OR, null);
        this.refs = refs;
        this.subElements = subElements;
    }

    public boolean removeRef (String refName) {
        boolean isRefRemoved = false;
        for (int i = 0; i < refs.size(); ) {
            if (refs.get(i).getVal().equals(refName)) {
                refs.remove(i);
                isRefRemoved = true;
            } else {
                i += 1;
            }
        }
        return isRefRemoved;
    }

    public void addRefs(Collection<ReferenceElement> targetRefs) {
       targetRefs.forEach(t -> {
           if (!refs.contains(t)) {
               refs.add(t);
           }
       });
    }

    @Override
    public SyntaxLispElement copy() {
        List<ReferenceElement> nwRefs = refs.stream()
                                            .map(e -> (ReferenceElement) e.copy())
                                            .collect(Collectors.toList());
        List<SyntaxLispElement> nwSubs = subElements.stream()
                                                    .map(SyntaxLispElement::copy)
                                                    .collect(Collectors.toList());
        return (new AmbiguousOrElement(nwRefs, nwSubs)).copyMatchPropertiesAndReturn(this);
    }

    @Override
    public boolean equalsTo(SyntaxLispElement o) {
        return false;
    }
}
